//
//  HeaderView.m
//  TaTaBang
//
//  Created by 李禹 on 15/8/16.
//  Copyright (c) 2015年 hailong. All rights reserved.
//

#import "HeaderView.h"
#import "TTButton.h"

@implementation HeaderViewItem


@end

@implementation HeaderView

+ (instancetype)headerViewWithItems:(NSArray<HeaderViewItem> *)items {
    return [[self alloc] initWithItems:items];
}

- (instancetype)initWithItems:(NSArray<HeaderViewItem> *)items {
    if (self = [super init]) {
        
        for (NSInteger index = 0; index < items.count; index++) {
            TTButton *btn = [[TTButton alloc] init];
            HeaderViewItem *item = items[index];
            [btn setTitle:item.title forState:UIControlStateNormal];
            [btn setImage:[UIImage imageNamed:item.norIcon] forState:UIControlStateNormal];
            btn.titleLabel.font = [UIFont systemFontOfSize:12];
            btn.imageView.contentMode = UIViewContentModeCenter;
            [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
            btn.tag = index;
            [btn addTarget:self action:@selector(buttomButtonDidClick:) forControlEvents:UIControlEventTouchUpInside];
            [self addSubview:btn];

        }
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    CGFloat width = self.width / self.subviews.count;
    CGFloat height = self.height;
    [self.subviews enumerateObjectsUsingBlock:^(UIButton *btn, NSUInteger idx, BOOL *stop) {
        btn.width = width;
        btn.height = height;
        btn.x = width * idx;
    }];
}

- (void)buttomButtonDidClick:(UIButton *)btn {
    if ([self.delegate respondsToSelector:@selector(headerView:didSelectedButtomViewType:)]) {
        [self.delegate headerView:self didSelectedButtomViewType:(HeaderViewType)btn.tag];
    }
}

@end
